package com.aiju.base.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.aiju.base.R;
import com.aiju.widgit.layout.AiJuTitleBarView;

/**
 * @author: 佐为
 * @date: 2016-11-10 09:53
 * @version: V1.0
 * @Description : 封装顶部带标题的activity
 */
public class BaseTitleActivity extends BaseActivity {

    protected final static String BACK_TITLE = "backTitle";

    protected AiJuTitleBarView mAiJuTitleBarView;

    protected LinearLayout mContentContainer;

    protected Activity mContext = this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initBaseViews();
    }

    @Override
    public void setContentView(int layoutResID) {
        View view = LayoutInflater.from(this).inflate(layoutResID, null);
        view.setLayoutParams(new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        mContentContainer.addView(view);
    }

    private void initBaseViews() {
        super.setContentView(getFrameLayoutId());
        mAiJuTitleBarView = getCommonToolBar();
        mContentContainer = getContentContainer();

        if (enableDefaultBack()) {
            setLeftView();
        } else {
            mAiJuTitleBarView.getLeftViewContainer().setVisibility(View.INVISIBLE);
        }
    }

    protected boolean enableDefaultBack() {
        return true;
    }

    protected int getFrameLayoutId() {
        return R.layout.activity_base_title;
    }

    protected AiJuTitleBarView getCommonToolBar() {
        return (AiJuTitleBarView) findViewById(R.id.toolbar);
    }

    protected LinearLayout getContentContainer() {
        return (LinearLayout) findViewById(R.id.ly_content);
    }

    /**
     * 跳转页面（带上一级标题）
     *
     * @param clas
     */
    public void startActivityForTitle(Class<?> clas) {
        Intent intent = new Intent(this, clas);
        intent.putExtra(BACK_TITLE, getCenterTitle());
        startActivity(intent);
    }

    /**
     * 跳转页面（带上一级标题）
     *
     * @param intent
     */
    public void startActivityForTitle(Intent intent) {
        intent.putExtra(BACK_TITLE, getCenterTitle());
        startActivity(intent);
    }

    protected void setCenterTitle(String title) {
        mAiJuTitleBarView.setCenterTitle(title);
    }

    protected void setLeftTitle(String title) {
        mAiJuTitleBarView.setLeftTitle(title);
    }

    protected void setRightTitle(String title) {
        mAiJuTitleBarView.setRightTitle(title);
    }

    /**
     * 获得左侧标题
     *
     * @return
     */
    protected String getBackTitel() {
        if (getIntent().hasExtra(BACK_TITLE)) {
            return getIntent().getStringExtra(BACK_TITLE);
        }
        return "";
    }

    /**
     * 获得中间标题
     *
     * @return
     */
    protected String getCenterTitle() {
        return mAiJuTitleBarView.getCenterTitle();
    }

    /**
     * 设置
     */
    protected void setLeftView() {
        mAiJuTitleBarView.setLeftOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!processBackPressed()) {
                    onBackPressed();
                }
            }
        });
        if (!TextUtils.isEmpty(getBackTitel())) {
            setLeftTitle(getBackTitel());
        }
    }

}
